package com.example.homemodule.viewmodel;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.examination.mlib.constants.ARouterConstants;
import com.example.homemodule.HomeUrl;
import com.example.homemodule.R;
import com.example.homemodule.adapter.HomeDrugListAdapter;
import com.example.homemodule.databinding.NewHomeDrugListFragmentDataBinding;
import com.example.homemodule.model.HomeDrugListModel;
import com.example.homemodule.model.bean.HomeCommodityGroupListEntity;
import com.examination.mlib.bean.HomeDrugEntity;
import com.jcodecraeer.xrecyclerview.CustomFooterViewCallBack;
import com.yilijk.base.base.BaseViewModel;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on
 */
public class HomeDrugListViewModel extends BaseViewModel<HomeDrugListModel> {
    NewHomeDrugListFragmentDataBinding binding;
    Context context;

    private HomeDrugListAdapter adapter;
    private List<HomeDrugEntity.DrugItemData> drugs = new ArrayList<>();

    private ImageView headerAdIv;
    private TextView footerInfoTv;
    private TextView loadingTv;
    private View progressView;
    private View headerView;
    private View footerView;


    private String groupId = "";
    public int page = 1;
    public int size = 10;
    public boolean isHasNext = true;
    
    public HomeDrugListViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    public void createRepository() {
        super.createRepository();
        repository = new HomeDrugListModel();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
    }

    public void initModel(NewHomeDrugListFragmentDataBinding binding, Context context){
        this.binding = binding;
        this.context = context;
    }

    public void initView(View baseView){
        binding.xrvDrugList.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        adapter = new HomeDrugListAdapter(context, drugs);
//        binding.xrvDrugList.setNestedScrollingEnabled(false);
        binding.xrvDrugList.setAdapter(adapter);
        binding.xrvDrugList.setPullRefreshEnabled(false);
        binding.xrvDrugList.setLoadingMoreEnabled(true);
        headerView = LayoutInflater.from(context).inflate(R.layout.header_drug_list, (ViewGroup) baseView, false);
        footerView = LayoutInflater.from(context).inflate(R.layout.footer_drug_list, (ViewGroup) baseView, false);
        binding.xrvDrugList.addHeaderView(headerView);
        headerAdIv = headerView.findViewById(R.id.iv_top);
        footerInfoTv = footerView.findViewById(R.id.tv_end);
        loadingTv = footerView.findViewById(R.id.tv_loading);
        progressView = footerView.findViewById(R.id.progress);
        binding.xrvDrugList.setFootView(footerView, new CustomFooterViewCallBack() {
            @Override
            public void onLoadingMore(View yourFooterView) {
                footerInfoTv.setVisibility(View.GONE);
                loadingTv.setVisibility(View.VISIBLE);
                progressView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadMoreComplete(View yourFooterView) {
                footerInfoTv.setVisibility(View.GONE);
                loadingTv.setVisibility(View.GONE);
                progressView.setVisibility(View.GONE);
            }

            @Override
            public void onSetNoMore(View yourFooterView, boolean noMore) {
//                footerInfoTv.setVisibility(View.VISIBLE);
                if (noMore) {
                    footerInfoTv.setVisibility(View.VISIBLE);
                    loadingTv.setVisibility(View.GONE);
                    progressView.setVisibility(View.GONE);
                } else {
                    footerInfoTv.setVisibility(View.GONE);
                    loadingTv.setVisibility(View.GONE);
                    progressView.setVisibility(View.GONE);
                }
            }
        });
    }

    public void initListener(){

    }

    public void refreshFragmentData(HomeCommodityGroupListEntity.Commodity commodity) {
            if (commodity != null) {
                groupId = commodity.getGroupId();
                if (!TextUtils.isEmpty(commodity.getImageUrl())) {
                    headerAdIv.setVisibility(View.VISIBLE);
                    GlideUtil.load(context, commodity.getImageUrl(), headerAdIv, R.drawable.icon_default_rectangle);
                    ClickUtils.setFastOnClickListener(headerAdIv, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!TextUtils.isEmpty(commodity.getUrl())) {
//                                ARouter.getInstance().build(ARouterConstants.HomeWebviewActivity)
//                                        .withString("activityId", commodity.getUrl())
//                                        .withBoolean("isShowLoading", true)
//                                        .navigation();
                                ARouter.getInstance().build(ARouterConstants.MineWebviewActivity)
//                                        .withString("title", entity.getData().getTitle())
                                        .withString("url", commodity.getUrl())
                                        .withBoolean("ishtml", false)
                                        .navigation();
                            }
                        }
                    });
                } else {

                }
                refreshList();
            }

    }

    public void refreshList() {
        page = 1;
        binding.xrvDrugList.setNoMore(false);
        getCommodityList();
    }
    public void getCommodityList() {
        if (TextUtils.isEmpty(groupId)){
            return;
        }
        ALog.e("oakkk", "commodity: " + groupId + " " + page + " " + size);
        RequestParams jsonObject = new RequestParams();
        jsonObject.put("groupId", groupId);
        jsonObject.put("pageIndex", page);
        jsonObject.put("pageSize", size);
        jsonObject.put("fromIndex", true);
        jsonObject.put("containVirtualService", false);
//        binding.xrvDrugList.setLoadingMoreEnabled(false);
        HttpUtils.get(HomeUrl.getCommodityList, jsonObject, new HttpCallBack<HomeDrugEntity>() {
            @Override
            public void onSuccess(HomeDrugEntity entity, int i) {
                if (entity.getCode() == 200 && entity.getData() != null) {

                    if (page == 1) {
                        drugs.clear();
                    }
                    binding.xrvDrugList.loadMoreComplete();
                    drugs.addAll(entity.getData().getDataList());
                    isHasNext = entity.getData().isNextPage();
//                    if (page == 1 && !isDrugsTlFixed) {
                    if (page == 1) {
//                        nestedScrollView.scrollTo(0, 0);
                        binding.xrvDrugList.scrollToPosition(0);
                    }
                    if (isHasNext) {
//                        page++;

                    } else {
//                        endTv.setVisibility(View.VISIBLE);
                        binding.xrvDrugList.setNoMore(true);

                    }
//                    binding.xrvDrugList.setLoadingMoreEnabled(isHasNext);
                } else {
                    binding.xrvDrugList.loadMoreComplete();
//                    binding.xrvDrugList.setLoadingMoreEnabled(true);
                }

                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String s, int i) {
                binding.xrvDrugList.loadMoreComplete();
                adapter.notifyDataSetChanged();
            }
        });
    }



}
